package com.example.FirstBook.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.FirstBook.MainActivity;
import com.example.myapplication.R;

public class GuideActivity extends AppCompatActivity {

    private TextView guideText;
    int time = 5;
    private TextView guideText2;
    SharedPreferences preferences;

    boolean a;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
        //隐藏标题栏
        ActionBar actionBar=getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }
        //读取sharePreferences存储的数据，判断是是否加载引导界面
        preferences=getSharedPreferences("data",MODE_PRIVATE);
        a=preferences.getBoolean("guide",false);
        final CountDownTimer timer=new CountDownTimer(6000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                guideText.setText(time-- + "");
                if (time == 0) {
                    intent();
                }
            }

            @Override
            public void onFinish() {

            }
        }.start();

        //设置点击事件，点击跳过直接跳转，不用等待
         guideText2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        timer.cancel();
               intent();
                    }
                });
    }

    /**
     * 初始化控件
     */
    private void initView() {
        guideText = (TextView) findViewById(R.id.guide_text1);
        guideText2 = (TextView) findViewById(R.id.guide_text2);

    }
    /*
    跳转界面
     */
    void intent(){
        if (a){
            startActivity(new Intent(GuideActivity.this, MainActivity.class));
            finish();
        }else {
            startActivity(new Intent(GuideActivity.this, GuideActivity2.class));
            finish();
        }
    }
}
